package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IDealService;
import com.woniu.supermarket.util.ExcelUtil;
import com.woniu.supermarket.vo.dealSummaryListVo;
import com.woniu.supermarket.vo.dealSummaryVo;
import com.woniu.supermarket.vo.dealVo;
import com.woniu.supermarket.vo.exportDealSummaryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("deal")
@Api(value = "销售交易汇总操作")
@Slf4j
public class DealController {

    private List<exportDealSummaryVo> lists ;

    @Autowired
    IDealService iDealService;

    @GetMapping("/showDeal")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<dealVo>> showDeal(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1")Integer now,
                                                     @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size,
                                                     @ApiParam(name = "salecodeOrName",value = "单号或商品名")@RequestParam(value = "salecodeOrName",defaultValue = "")String salecodeOrName){

        ResultResponse<PageInfo<dealVo>> result = null;
//        System.out.println("salesCode=============="+salecode);
        try {
            PageInfo<dealVo> pageInfo = iDealService.findByCons(now, size, salecodeOrName);
            result = new ResultResponse<>(200,"查询销售订单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询销售订单失败");
        }
        return result;
    }

    @GetMapping("/advancedSearch")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<dealVo>> showDeal(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1")Integer now,
                                                     @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size,
                                                     @ApiParam(name = "dealStartTime",value = "开始时间")@RequestParam(name = "dealStartTime",defaultValue = "")@DateTimeFormat(pattern = "yyyy-MM-dd")Date dealStartTime,
                                                     @ApiParam(name = "dealEndTime",value = "结束时间")@RequestParam(name = "dealEndTime",defaultValue = "")@DateTimeFormat(pattern = "yyyy-MM-dd") Date dealEndTime,
                                                     @ApiParam(name = "memberTel",value = "会员电话")@RequestParam(name = "memberTel",defaultValue = "")String memberTel,
                                                     @ApiParam(name = "salecodeOrName",value = "商品编码/名称")@RequestParam(name = "salecodeOrName",defaultValue = "")String salecodeOrName,
                                                     @ApiParam(name = "shid",value = "门店id")@RequestParam(name = "shid",defaultValue = "")Integer shid,
                                                     @ApiParam(name = "staff",value = "操作人员")@RequestParam(name = "staff",defaultValue = "")String staff,
                                                     @ApiParam(name = "typeOrder",value = "订单类型")@RequestParam(name = "typeOrder",defaultValue = "0")Integer typeOrder
                                                     ){

        ResultResponse<PageInfo<dealVo>> result = null;
        System.out.println("dasv=============="+dealStartTime);
        try {
            PageInfo<dealVo> pageInfo = iDealService.advancedsearchByCons(now, size, dealStartTime, dealEndTime, memberTel, salecodeOrName, shid, staff, typeOrder);
            result = new ResultResponse<>(200,"查询销售订单成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询销售订单失败");
        }
        return result;
    }


    @GetMapping("/showDealSummary")
    @ApiOperation(value = "分页查询操作")
    public ResultResponse<PageInfo<dealSummaryVo>> showSummaryDeal(@ApiParam(name = "now",value = "当前页")@RequestParam(name = "now",defaultValue = "1")Integer now,
                                                     @ApiParam(name = "size",value = "页大小")@RequestParam(name = "size",defaultValue = "3")Integer size,
                                                     @ApiParam(name = "shopNameOrCode",value = "单号或商品名")@RequestParam(value = "shopNameOrCode",defaultValue = "")String shopNameOrCode,
                                                     @ApiParam(name = "searchType",value = "按什么类型来查")@RequestParam(name = "searchType",defaultValue = "0")Integer searchType

    ){

        ResultResponse<PageInfo<dealSummaryVo>> result = null;
        System.out.println("shopNameOrCode=============="+shopNameOrCode);
        System.out.println("searchType=============="+searchType);
        try {
            PageInfo<dealSummaryVo> pageInfo = iDealService.querySummaryDeal(now, size, shopNameOrCode, searchType);
            result = new ResultResponse<>(200,"查询销售汇总成功",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询销售汇总失败");
        }
        return result;
    }


    @PostMapping(value = "/exportExcel")
    @ApiOperation(value = "导出")
    public ResultResponse<Void> exportExcel(HttpServletResponse response , @RequestBody dealSummaryListVo dealSummaryListVo){
        ResultResponse<Void> result = null;
        System.out.println("dsl==============");
        try {
            dealSummaryListVo.printlist();
            List<exportDealSummaryVo> dealSummaryList = dealSummaryListVo.getDealSummaryList();
            lists = dealSummaryList;
            iDealService.getlist(dealSummaryList);

            result = new ResultResponse<>(200,"前台数据传入成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"前台数据传入失败");
        }
        return result;
    }

    /*@PostMapping(value = "/exportExcel2",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "导出")
    public ResultResponse<Void> exportExcel(HttpServletResponse response){
        ResultResponse<Void> result = null;
        try {
            ExcelUtil.writeExcel(response,lists);

            result = new ResultResponse<>(200,"导出销售汇总成功");
        }catch (IOException e1){
            e1.printStackTrace();
            result = new ResultResponse<>(204,"输出流错误");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"导出销售汇总失败");
        }
        return result;
    }*/


    @PostMapping(value = "/exportExcel2",produces="application/json")
    @ApiOperation(value = "导出")
    public ResultResponse<Void> exportExcel(HttpServletResponse response){
        ResultResponse<Void> result = null;
        try {
            iDealService.exportExcel(response);
            result = new ResultResponse<>(200,"导出销售汇总成功");
        }catch (IOException e1){
            e1.printStackTrace();
            result = new ResultResponse<>(204,"输出流错误");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"导出销售汇总失败");
        }
        return result;
    }


}
